/*
 * @Author: tangwc
 * @Date: 2023-03-23 11:54:03
 * @LastEditors: tangwc
 * @LastEditTime: 2023-03-28 22:00:44
 * @Description:
 * @FilePath: \stm32_u8g2_demo\Core\Src\stm32_u8g2.c
 *
 *  Copyright (c) 2023 by chipsea, All Rights Reserved.
 */
#include <stdint.h>
#include "stm32_u8g2.h"
#include "tim.h"
#include "u8x8.h"

uint8_t u8x8_stm32_gpio_and_delay(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_prt)
{
    switch (msg)
    {
    case U8X8_MSG_GPIO_AND_DELAY_INIT:     
        HAL_Delay(200);           
        break;
    case U8X8_MSG_DELAY_MILLI:
        HAL_Delay(arg_int);
        break;
    case U8X8_MSG_DELAY_NANO: 
        break;
    case U8X8_MSG_DELAY_100NANO:  
        __NOP();  
        break;
    case U8X8_MSG_GPIO_SPI_DATA:
        (arg_int) ? OLED_SDA_SET : OLED_SDA_CLR;
        break;
    case U8X8_MSG_GPIO_SPI_CLOCK:
        (arg_int) ? OLED_CLK_SET : OLED_CLK_CLR;
        break;
    case U8X8_MSG_GPIO_CS:
        (arg_int) ? OLED_CS_SET : OLED_CS_CLR;
        break;
    case U8X8_MSG_GPIO_DC:
        (arg_int) ? OLED_DC_SET : OLED_DC_CLR;
        break;
    case U8X8_MSG_GPIO_RESET:
        (arg_int) ? OLED_RES_SET : OLED_RES_CLR;
        break;
    default:
        return 0;//A message was received which is not implemented, return 0 to indicate an error
    }
    return 1;
}

void u8g2_init(u8g2_t* u8g2)
{
    u8g2_Setup_ssd1306_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi, u8x8_stm32_gpio_and_delay);// 初始化 u8g2 结构体
    u8g2_InitDisplay(u8g2);     // 根据所选的芯片进行初始化工作，初始化完成后，显示器处于关闭状态
    u8g2_SetPowerSave(u8g2, 0);  // 打开显示器
    u8g2_ClearBuffer(u8g2);
}

void u8g2_test_demo(u8g2_t* u8g2)
{
    u8g2_FirstPage(u8g2);
    do {
        u8g2_SetFontMode(u8g2, 1);              /*字体模式选择*/
        u8g2_SetFontDirection(u8g2, 0);         /*字体方向选择*/
        u8g2_SetFont(u8g2, u8g2_font_inb24_mf); /*字库选择*/
        u8g2_DrawStr(u8g2, 0, 20, "U");

        u8g2_SetFontDirection(u8g2, 1);
        u8g2_SetFont(u8g2, u8g2_font_inb30_mn);
        u8g2_DrawStr(u8g2, 21, 8, "8");

        u8g2_SetFontDirection(u8g2, 0);
        u8g2_SetFont(u8g2, u8g2_font_inb24_mf);
        u8g2_DrawStr(u8g2, 51, 30, "g");
        u8g2_DrawStr(u8g2, 67, 30, "\xb2");

        u8g2_DrawHLine(u8g2, 2, 35, 47);
        u8g2_DrawHLine(u8g2, 3, 36, 47);
        u8g2_DrawVLine(u8g2, 45, 32, 12);
        u8g2_DrawVLine(u8g2, 46, 33, 12);

        u8g2_SetFont(u8g2, u8g2_font_4x6_tr);
        u8g2_DrawStr(u8g2, 1, 54, "github.com/olikraus/u8g2");
    } while (u8g2_NextPage(u8g2));
}